package cn.com.darly.vpm.bizapi;

import cn.com.darly.vpm.business.amuse.bean.Amuse;
import cn.com.darly.vpm.business.amuse.service.AmuseService;
import cn.com.darly.vpm.framework.base.BaseController;
import cn.com.darly.vpm.security.system.service.SystemLogService;
import cn.com.darly.vpm.security.user.api.LoginApi;
import cn.com.darly.vpm.security.user.bean.User;
import cn.com.darly.vpm.security.user.cfg.ApplicationConst;
import cn.com.darly.vpm.security.user.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author : ZhangYuHui
 * Date : 2017/11/13
 * TODO :
 */
@Service
public class LoginBiz extends BaseController implements LoginApi {


    @Resource
    SystemLogService systemLogService;


    //----------amuse------------
    @Autowired
    AmuseService amuseService;
    @Autowired
    UserService userService;

    @Override
    public ModelMap login(String error) {
        ModelMap model = new ModelMap();
        model.addAttribute(ApplicationConst.getApplicationName(), applicationName);
        model.addAttribute(ApplicationConst.getPageTitle(), applicationName);
        model.addAttribute(ApplicationConst.getResourceUrl(), resourceUrl);
        logger.info("--->[方法 login 运行中...]");
        if ("true".equals(error)) {
            this.logger.info("用户登陆失败！");
        }
        model.addAttribute(ApplicationConst.getForwordUrl(), "login");
        return model;
    }

    @Override
    public ModelMap sameuser() {
        ModelMap model = new ModelMap();
        model.addAttribute(ApplicationConst.getApplicationName(), applicationName);
        model.addAttribute(ApplicationConst.getPageTitle(), "异地登录");
        model.addAttribute(ApplicationConst.getResourceUrl(), resourceUrl);
        logger.info("--->[方法 sameuser 运行中...]");
        model.addAttribute(ApplicationConst.getForwordUrl(), "/error/sameuser");
        return model;
    }

    @Override
    public ModelMap timeout() {
        ModelMap model = new ModelMap();
        model.addAttribute(ApplicationConst.getApplicationName(), applicationName);
        model.addAttribute(ApplicationConst.getPageTitle(), "登录超时");
        model.addAttribute(ApplicationConst.getResourceUrl(), resourceUrl);
        logger.info("--->[方法 timeout 运行中...]");
        model.addAttribute(ApplicationConst.getForwordUrl(), "/error/timeout");
        return model;
    }

    @Override
    public ModelMap forwardLogin() {
        ModelMap model = new ModelMap();
        model.addAttribute(ApplicationConst.getApplicationName(), applicationName);
        model.addAttribute(ApplicationConst.getPageTitle(), applicationName);
        model.addAttribute(ApplicationConst.getResourceUrl(), resourceUrl);
        logger.info("--->[方法 forwardLogin 运行中...]");
        model.addAttribute(ApplicationConst.getForwordUrl(), "login/relogin");
        return model;
    }

    @Override
    public ModelMap index() {
        ModelMap model = new ModelMap();
        model.addAttribute(ApplicationConst.getApplicationName(), applicationName);
        model.addAttribute(ApplicationConst.getPageTitle(), applicationName);
        model.addAttribute(ApplicationConst.getResourceUrl(), resourceUrl);
        model.addAttribute("storename", "达理全球连锁商城");
        //可以在这里添加展示内容。比如现在展示审核通过的Amuse
        PageInfo<Amuse> page =  amuseService.getAmuse( 1, 10);
        List<Amuse> data = page.getList();
        //通过话题中的用户ID。将用户信息封装进对象中。并传递到前端
        for (Amuse amuse: data) {
            User user = userService.findUserByID(amuse.getAuthor());
            if (user!=null){
                amuse.setUser(user);
            }
        }
        model.addAttribute("Amuse",data);
        model.addAttribute(ApplicationConst.getForwordUrl(), "login/index");
        return model;
    }

    @Override
    public ModelMap map() {
        ModelMap model = new ModelMap();
        model.addAttribute(ApplicationConst.getApplicationName(), applicationName);
        model.addAttribute(ApplicationConst.getPageTitle(), "地图模块");
        model.addAttribute(ApplicationConst.getResourceUrl(), resourceUrl);
        model.addAttribute(ApplicationConst.getForwordUrl(), "map/map");
        return model;
    }

    @Override
    public ModelMap home() {
        ModelMap model = new ModelMap();
        model.addAttribute(ApplicationConst.getApplicationName(), applicationName);
        model.addAttribute(ApplicationConst.getPageTitle(), applicationName+"首页");
        model.addAttribute(ApplicationConst.getResourceUrl(), resourceUrl);
        model.addAttribute(ApplicationConst.getForwordUrl(), "home");
        return model;
    }
}